package com.sunday.aomicaichang.adpter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.model.AreaType;
import com.sunday.aomicaichang.model.Village;
import com.sunday.aomicaichang.ui.widgets.SidebarView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.app.Activity.RESULT_OK;


public class SelectAreaChildAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


//    private List<Area.ChildrenBean> dataSet;
    private Context mContext;
    private List<AreaType> dataSet;
    private Activity mActivity;
    private SidebarView sidebarView;
    public SelectAreaChildAdapter(Context context, Activity mActivity, List<AreaType> datas,SidebarView sidebarView) {
        this.mContext = context;
        this.dataSet = datas;
        this.mActivity=mActivity;
        this.sidebarView=sidebarView;
        setData(dataSet);
    }

    public void setData(List<AreaType> dataSet){
        this.dataSet=dataSet;
        List<String> sections=new ArrayList<>();
        if(sections!=null)
            sections.clear();
        for (AreaType areaType:dataSet){
            if (areaType.getAreaType()==1){
                sections.add(areaType.getTit());
            }
        }
        sidebarView.setSections(sections);
        notifyDataSetChanged();
    }




    @Override
    public int getItemViewType(int position) {
        return dataSet.get(position).getAreaType();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 1) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_area_content_tit, parent, false);
            return new TopViewHolder(v);
        } else {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_area_content, parent, false);
            return new ItemViewHolder(v);
        }

    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TopViewHolder) {
            String tit=dataSet.get(position).getTit();
            TopViewHolder viewHolder = (TopViewHolder) holder;
            if(tit!=null)
                viewHolder.tit.setText(tit);
        }else  {
            final Village.ChildrenBeanX.ChildrenBean childrenBean = dataSet.get(position).getChildrenBean();
            ItemViewHolder viewHolder = (ItemViewHolder) holder;
            if(childrenBean==null)
                return;
            if(childrenBean.getName()!=null)
            viewHolder.content.setText(childrenBean.getName());
            viewHolder.content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("area", childrenBean);
                    mActivity.setResult(RESULT_OK, intent);
                    mActivity.finish();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return dataSet == null ? 0 : dataSet.size();
    }

    public class TopViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tit)
        TextView tit;

        public TopViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.content)
        TextView content;
        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    SparseIntArray positionOfSection;
    SparseIntArray sectionOfPosition;
    //记录分组信息
    public Object[] getSections() {
        positionOfSection = new SparseIntArray();
        sectionOfPosition = new SparseIntArray();
        int count = dataSet.size();
        List<String> list = new ArrayList<String>();
        positionOfSection.put(0, 0);
        sectionOfPosition.put(0, 0);
        AreaType areaType;
        int section = 0;
        for (int i = 1; i < count; i++) {
            if (getItemViewType(i)==1){
                list.add(dataSet.get(i).getTit());
                section++;
                positionOfSection.put(section, i);
            }
            sectionOfPosition.put(i, section);
        }
        return list.toArray(new String[list.size()]);
    }

    public int getSectionForPosition(int position) {
        return sectionOfPosition.get(position);
    }
    public int getPositionForSection(int section) {
        return positionOfSection.get(section);
    }


}
